package com.lagou.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.lagou.entity.CourseComment;
import com.lagou.service.CourseCommentService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("course")
public class CourseCommentController {
    @Reference
    private CourseCommentService courseCommentService;

    @GetMapping("comment/saveCourseComment")
    public Object saveCourseComment() {
        CourseComment comment = new CourseComment();
        comment.setCourseId(7); //课程编号
        comment.setSectionId(8); //章节编号
        comment.setLessonId(10); //课时编号
        comment.setUserId(100030011); //用户编号
        comment.setUserName("往事如烟"); //用户昵称
        comment.setParentId(0); //没有父id
        comment.setComment("老铁666!"); //留言内容
        comment.setType(0); //0用户留言
        comment.setLastOperator(100030011);

        Integer i = courseCommentService.saveComment(comment);
        System.out.println("i = " + i);

        return i;
    }

    @GetMapping("comment/getCourseCommentList/{courseId}/{pageIndex}/{pageSize}")
    public List<CourseComment> getCommentsByCourseId(@PathVariable Integer courseId, @PathVariable Integer pageIndex,
                                                     @PathVariable Integer pageSize) {
        Integer offset = Math.max((pageIndex - 1), 0) * pageSize;

        return courseCommentService.getCommentsByCourseId(courseId, offset, pageSize);
    }

    @GetMapping("comment/saveFavorite/{commentId}/{userId}")
    public Integer saveFavorite(@PathVariable Integer commentId, @PathVariable Integer userId){
        return courseCommentService.saveFavorite(commentId, userId);
    }

    @GetMapping("comment/cancelFavorite/{commentId}/{userId}")
    public Integer cancelFavorite(@PathVariable Integer commentId, @PathVariable Integer userId) {
        return courseCommentService.cancelFavorite(commentId, userId);
    }
}
